/*
 *  Copyright (C) 2010  Noxious Productions
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef __XML_FILE_H__
#define __XML_FILE_H__

#include <tinyxml.h>
#include <string>
#include <iostream>
using namespace std;

/**
 *  A simple xml parser. Reads and writes attributes from xml elements.
 *  This class is used to load informations about items, creatures, maps, etc. from different files.
 */

class XMLFile
{
public:

    XMLFile();
    ~XMLFile();
    
    /**
     *  Loads the xml file.
     *  @param fileName The path to the file.
     *  @return true if successful. If this function returns false no other methods will work.
     */
    bool LoadFile(const string &fileName);
    
    /**
     *  Set the active element back to the root element.
     *  @return true if successful.
     */
    bool ResetToRoot();

    /**
     *  Set the active element.
     *  @param elementName Name of the element. Must be a child element of the current active element.
     *  @return true if successful.
     */
    bool SetActiveElement(const string &elementName);

    /**
     *  Set the active element to the next sibling.
     *  @return true if successful.
     */
    bool Next();

    /**
     *  Get the value of an attribute.
     *  @param attributeName Name of the attribute you want to get the value of.
     *  @return the value of the element if aviable. Will return '' if not aviable. Will never return NULL.
     */
    const char * GetAttribute(const string &attributeName);

    /**
     *  Get the value of an attribute.
     *  @param attributeName Name of the attribute you want to get the value of.
     *  @param defaultValue Default value of the attribute.
     *  @return the value of the element if aviable. Return defaultValue if the attribute is not aviable.
     */
    int GetAttribute(const string &attributeName, int defaultValue);

    /**
     *  Get the value of an attribute.
     *  @param attributeName Name of the attribute you want to get the value of.
     *  @param defaultValue Default value of the attribute.
     *  @return the value of the element if aviable. Return defaultValue if the attribute is not aviable.
     */
    double GetAttribute(const string &attributeName, double defaultValue);

    /**
     *  Get the text of an element.
     *  @return the text of the element if aviable. Will return '' if not aviable. Will never return NULL.
     */
    const char * GetText();

    /**
     *  Get the value of an attribute.
     *  @param text New text of the element.
     *  @return true if successful.
     */
    bool SetText(const string &text);

    /**
     *  Get the value of an attribute.
     *  @param attributeName Name of the attribute you want to set the value of.
     *  @param value New value of the attribute.
     */
    void SetAttribute(const string &attributeName, const string &value);

    /**
     *  Get the value of an attribute.
     *  @param attributeName Name of the attribute you want to set the value of.
     *  @param value New value of the attribute.
     */
    void SetAttribute(const string &attributeName, int value);

    /**
     *  Get the value of an attribute.
     *  @param attributeName Name of the attribute you want to set the value of.
     *  @param value New value of the attribute.
     */
    void SetAttribute(const string &attributeName, double value);

private:

    TiXmlDocument doc;      ///<  TiXmlDocument object. Handles loading and safing the xml file. From TinyXml.
    TiXmlElement * root;    ///<  Object of the first/root element in the xml file.
    TiXmlElement * active;  ///<  Object of the active element.

    bool error;             ///<  True if a fatal error occured.

};

#endif
